package com.eyedog.video.videodetail;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import com.eyedog.video.R;

/**
 * created by jw200 at 2019/5/17 13:05
 **/
public abstract class CommonPopupWindow {

    protected Context context;
    protected View contentView;
    protected PopupWindow mInstance;

    public CommonPopupWindow(Context c, int layoutRes, int w, int h) {
        context = c;
        contentView = LayoutInflater.from(c).inflate(layoutRes, null, false);
        initView();
        initEvent();
        mInstance = new PopupWindow(contentView, w, h, true);
        initWindow();
    }

    public View getContentView() {
        return contentView;
    }

    public PopupWindow getPopupWindow() {
        return mInstance;
    }

    public void setProgress(int progress) {
        if (contentView != null) {
            if (progress <= 0) {
                progress = 0;
            } else if (progress >= 100) {
                progress = 100;
            }
            SeekBar seekBar = (SeekBar) contentView.findViewById(R.id.volume_bar);
            seekBar.setProgress(progress);
        }
    }

    protected abstract void initView();

    protected abstract void initEvent();

    protected void initWindow() {
        mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mInstance.setOutsideTouchable(true);
        mInstance.setTouchable(true);
    }

    boolean hasShowed = false;

    public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {
        int[] offset = layoutGravity.getOffset(anchor, mInstance);
        mInstance.showAsDropDown(anchor, hasShowed ? offset[0] : (offset[0] + xmerge),
            hasShowed ? offset[1] : (offset[1] + ymerge));
        hasShowed = true;
    }

    public void showAsDropDown(View anchor, int xoff, int yoff) {
        mInstance.showAsDropDown(anchor, xoff, yoff);
    }

    public void showAtLocation(View parent, int gravity, int x, int y) {
        mInstance.showAtLocation(parent, gravity, x, y);
    }
}
